<template>
  <el-dialog v-model="dialogVisible" :title="title" width="50%" :before-close="handleClose">
    <div class="tab6-dialog-hetong">
      <el-form ref="ruleFormRef" :model="ruleForm" :rules="rules" label-width="110px" inline>
        <el-form-item prop="customer_id" label="客户名称">
          <el-input placeholder="客户名称" disabled v-model="dataInfo.name" />
        </el-form-item>
        <el-form-item prop="order_no" label="相关合同">
          <el-select style="width: 100%" v-model="ruleForm.order_no" placeholder="相关合同" filterable clearable>
            <el-option v-for="item in contractOptions" :key="item.id" :label="item.order_no" :value="item.order_no" />
          </el-select>
        </el-form-item>
        <el-form-item prop="" label="备注" style="width: 100%">
          <el-input placeholder="备注" type="textarea" :rows="2" v-model="ruleForm.remark" />
        </el-form-item>
        <el-form-item prop="" label="付款周期" style="width: 100%">
          <el-table :data="planTable" border>
            <el-table-column prop="a" label="期次">
              <template #default="{ row }">
                <el-input style="width: 100%" placeholder="期次" v-model="row.num" />
              </template>
            </el-table-column>
            <el-table-column prop="b" label="金额(元)">
              <template #default="{ row }">
                <el-input style="width: 100%" placeholder="金额" v-model="row.plan_money" />
              </template>
            </el-table-column>
            <el-table-column prop="c" label="计划回款日期">
              <template #default="{ row }">
                <el-date-picker style="width: 100%" v-model="row.plan_date" type="datetime" placeholder="计划回款日期"
                  value-format="YYYY-MM-DD HH:mm:ss" />
              </template>
            </el-table-column>

            <el-table-column label="操作" width="80">
              <template #default="scoped">
                <el-button type="danger" @click="collectionDel(scoped)" link>
                  删除
                </el-button>
              </template>
            </el-table-column>
          </el-table>
          <div style="margin: 10px 0">
            <el-button type="primary" @click="addCollection">
              + 新增期次
            </el-button>
          </div>
        </el-form-item>
      </el-form>
    </div>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="dialogVisible = false">关闭</el-button>
        <el-button type="primary" @click="submit"> 提交 </el-button>
      </span>
    </template>
  </el-dialog>
</template>
<script setup>
import { receivablesPlanCreateApi } from "@/plugins/api/customer/manage/tab6";
import { optionsApi as contractApi } from "@/plugins/api/customer/manage/tab4";
const props = defineProps({
  dataInfo: {
    type: Object,
    default: {},
  },
});
// 相关合同
let contractOptions = ref([]);
function getContract() {
  contractApi({
    customer_id: props.dataInfo.id,
  }).then((data) => {
    contractOptions.value = data;
  });
}
let ruleForm = ref({});
let rules = {
  name: [
    {
      required: true,
      message: "",
      trigger: "blur",
    },
  ],
  own_type: [
    {
      required: true,
      message: "客户类型",
      trigger: "change",
    },
  ],
};
let dialogVisible = ref(false);
function handleClose(done) {
  ElMessageBox.confirm("是否确定关闭此对话框?")
    .then(() => {
      done();
    })
    .catch(() => { });
}
let title = ref("");
let submitType = ref("");
let ruleFormRef = ref(null);
function initFun(type, row) {
  if (ruleFormRef.value) {
    ruleFormRef.value.resetFields();
  }
  ruleForm.value = {};
  planTable.value = [];
  title.value = "新增";
  dialogVisible.value = true;
  getContract();
}

const emit = defineEmits(["refreshList"]);
function submit() {
  if (!ruleFormRef.value) return;
  ruleFormRef.value.validate((valid, fields) => {
    if (valid) {
      let obj = { ...ruleForm.value };
      obj.customer_id = props.dataInfo.id;
      obj.plans = planTable.value;
      receivablesPlanCreateApi(obj).then((res) => {
        ElMessage.success("新增成功！");
        dialogVisible.value = false;
        emit("refreshList");
      });
    }
  });
}

// 回款信息
let planTable = ref([]);
function collectionDel(row) {
  planTable.value.splice(row.$index, 1);
}
function addCollection() {
  planTable.value.push({});
}

onMounted(() => { });
defineExpose({
  initFun,
});
</script>
<style scoped lang="scss">
.tab6-dialog-hetong {
  h4 {
    margin: 0;
    margin-bottom: 20px;
  }

  :deep(.el-form-item) {
    width: 50%;
    margin-right: 0;
  }

  .goods-head {
    display: flex;
    justify-content: space-between;
    margin-bottom: 10px;

    span {
      margin-right: 20px;
    }
  }
}
</style>
